package com.fin.zw.intelligentbot.controller;

import com.fin.zw.intelligentbot.annotation.CurrentUser;
import com.fin.zw.intelligentbot.model.request.StrategyRequest;
import com.fin.zw.intelligentbot.model.response.StrategyResponse;
import com.fin.zw.intelligentbot.model.session.SessionModel;
import com.fin.zw.intelligentbot.service.StrategyInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;


@RestController
@RequestMapping("/strategyInfo")
@Api(value = "策略信息管理", description = "策略信息管理")
public class StrategyInfoController {

    @Autowired
    private StrategyInfoService strategyInfoService;

    @GetMapping("/fetchPage")
    @ApiOperation(value = "查询所有的策略信息", notes = "查询所有的策略信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query", value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query", value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query",
                    value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<Page<StrategyResponse>> queryStategyInfo(@RequestParam(value = "name", required = false) String name,
                                                                   @RequestParam(value = "categoryId", required = false) String categoryId,
                                                                   Pageable pageable, @ApiIgnore @CurrentUser SessionModel sessionModel) {
        Page<StrategyResponse> page = strategyInfoService.getStrategyInfo(name, categoryId, sessionModel, pageable);

        return ResponseEntity.ok().body(page);
    }

    @GetMapping("/fetch")
    @ApiOperation(value = "查询对应的场景信息", notes = "查询对应的场景信息")
    public ResponseEntity<List<StrategyResponse>> fetch(@ApiIgnore @CurrentUser SessionModel sessionModel) {
        List<StrategyResponse> list = strategyInfoService.getStrategyList(sessionModel);
        return ResponseEntity.ok().body(list);
    }

    @PostMapping("/add")
    @ApiOperation(value = "策略信息的添加", notes = "策略信息的添加")
    public ResponseEntity<Void> createStrategy(@RequestBody StrategyRequest request,
                                               @ApiIgnore @CurrentUser SessionModel sessionModel) {
        strategyInfoService.createStrategy(request, sessionModel);
        return ResponseEntity.ok().build();
    }

    @RequestMapping(value = "/update", method = {RequestMethod.PUT, RequestMethod.PATCH})
    @ApiOperation(value = "策略信息的添加", notes = "策略信息的添加")
    public ResponseEntity<Void> updateStrategy(@RequestBody StrategyRequest request) {
        strategyInfoService.updateStrategy(request);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "策略信息的删除", notes = "策略信息的删除")
    public ResponseEntity<Void> deleteStrategy(@PathVariable String id) {
        strategyInfoService.deleteStrategy(id);
        return ResponseEntity.ok().build();
    }
}
